-
Notifications
You must be signed in to change notification settings - Fork 9
/
PDFBuild+Background.swift
51 lines (44 loc) · 1.73 KB
/
PDFBuild+Background.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// PDFBuild+Background.swift
// Chord Provider
//
// © 2024 Nick Berendsen
//
import AppKit
extension PDFBuild {
// MARK: A PDF **background** element
/// A PDF **background** element
///
/// Fill a ``PDFElement`` with a ``SWIFTColor`` background
class Background: PDFElement {
/// The `NSColor` for the background
let color: NSColor
/// The ``PDFElement`` to add on top of the background
let element: PDFElement
/// Init the **background** element
/// - Parameters:
/// - color: The `NSColor` for the background
/// - element: The ``PDFElement`` to add on top of the background
init(color: NSColor, _ element: PDFElement) {
self.color = color
self.element = element
}
/// Draw the **background** element
/// - Parameters:
/// - rect: The available rectangle
/// - calculationOnly: Bool if only the Bounding Rect should be calculated
/// - pageRect: The page size of the PDF document
func draw(rect: inout CGRect, calculationOnly: Bool, pageRect: CGRect) {
/// Render the background below the element
if !calculationOnly, let context = NSGraphicsContext.current?.cgContext {
let tempRect = calculateDraw(rect: rect, elements: [element], pageRect: pageRect)
var fillRect = rect
fillRect.size.height = rect.height - tempRect.height
context.setFillColor(color.cgColor)
context.fill(fillRect)
}
/// Draw whatever goes on top of the background
element.draw(rect: &rect, calculationOnly: calculationOnly, pageRect: pageRect)
}
}
}